/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.common.session.stateless;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import merlion.common.entity.DocFile;
import merlion.common.entity.Staff;
import merlion.crm.sopm.entity.PurchaseOrder;
import merlion.crm.sopm.entity.SalesInquiry;
import merlion.crm.sopm.entity.SalesOrder;
import merlion.crm.sopm.entity.SalesQuotation;
import merlion.frm.bpm.entity.Invoice;
import merlion.frm.bpm.entity.Receipt;
import merlion.frm.bpm.entity.SupplierInvoice;
import merlion.frm.bpm.entity.SupplierReceipt;
import merlion.scm.rmpm.entity.RawMaterialPurchaseOrder;
import merlion.scm.rmpm.entity.RawMaterialSalesOrder;

/**
 *
 * @author Zhang Ying
 */
@Stateless
public class FileManagerSession implements FileManagerSessionLocal {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public boolean saveProfilePicture(Long staffId, DocFile myDoc) {
        try {
            Staff staff = entityManager.find(Staff.class, staffId);
            if (staff.getProfilePicture() != null) {
                entityManager.remove(staff.getProfilePicture());
            }
            staff.setProfilePicture(myDoc);
            entityManager.merge(staff);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public DocFile getProfileImageById(String idStr) {
        Long id = Long.valueOf(idStr);
        return entityManager.find(DocFile.class, id);
    }

    @Override
    public boolean haveProfilePage(Long id) {
        Staff staff = entityManager.find(Staff.class, id);
        return (staff.getProfilePicture() != null);
    }

    @Override
    public DocFile getProfileImage(Long staffId) {
        Staff staff = entityManager.find(Staff.class, staffId);
        return staff.getProfilePicture();
    }

    @Override
    public boolean uploadFileIntoPurchaseOrder(DocFile myDoc, String tempPOID) {
        try {
            PurchaseOrder po = (PurchaseOrder) entityManager.find(PurchaseOrder.class, Long.valueOf(tempPOID));
            if (po.getUploadedImage() != null) {
                entityManager.remove(po.getUploadedImage());
            }
            po.setUploadedImage(myDoc);
            entityManager.merge(po);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean uploadFileIntoSalesInquiry(DocFile myDoc, String tempPOID) {
        try {
            SalesInquiry si = (SalesInquiry) entityManager.find(SalesInquiry.class, Long.valueOf(tempPOID));
            if (si.getReqForInquiry() != null) {
                entityManager.remove(si.getReqForInquiry());
            }
            si.setReqForInquiry(myDoc);
            entityManager.merge(si);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean uploadFileIntoSalesQuotation(DocFile myDoc, String tempPOID) {
        try {
            SalesQuotation sq = (SalesQuotation) entityManager.find(SalesQuotation.class, Long.valueOf(tempPOID));
            if (sq.getUploadedImage() != null) {
                entityManager.remove(sq.getUploadedImage());
            }
            sq.setUploadedImage(myDoc);
            entityManager.merge(sq);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }
    
    @Override
    public SupplierInvoice uploadFileIntoSupplierInvoice(DocFile myDoc, SupplierInvoice invoice) {
        try {
            if (invoice.getUploadedImage() != null) {
                entityManager.remove(invoice.getUploadedImage());
            }
            invoice.setUploadedImage(myDoc);
            return invoice;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
    
    @Override
    public SupplierReceipt uploadFileIntoSupplierReceipt(DocFile myDoc, SupplierReceipt receipt) {
        try {
            if (receipt.getUploadedImage() != null) {
                entityManager.remove(receipt.getUploadedImage());
            }
            receipt.setUploadedImage(myDoc);
            return receipt;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
    
    @Override
    public RawMaterialSalesOrder uploadFileIntoRawMaterialSalesOrder(DocFile myDoc, RawMaterialSalesOrder rmso){
        try {
            if (rmso.getUploadedImage() != null) {
                entityManager.remove(rmso.getUploadedImage());
            }
            rmso.setUploadedImage(myDoc);
            return rmso;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public SalesQuotation getSalesQuotationById(Long id) {
        return entityManager.find(SalesQuotation.class, id);
    }

    @Override
    public SalesOrder getSalesOrderById(Long id) {
        return entityManager.find(SalesOrder.class, id);
    }

    @Override
    public Invoice getInvoiceById(Long id) {
        return entityManager.find(Invoice.class, id);
    }

    @Override
    public Receipt getReceiptById(Long id) {
        return entityManager.find(Receipt.class, id);
    }
    
    @Override
    public DocFile getDocFileSQ(String tempPOID) {
        SalesQuotation sq = (SalesQuotation) entityManager.find(SalesQuotation.class, Long.valueOf(tempPOID));
        return sq.getUploadedImage();
    }

    @Override
    public DocFile getDocFileSI(String tempPOID) {
        SalesInquiry si = (SalesInquiry) entityManager.find(SalesInquiry.class, Long.valueOf(tempPOID));
        return si.getReqForInquiry();
    }

    @Override
    public DocFile getDocFilePO(String tempPOID) {
        PurchaseOrder po = (PurchaseOrder) entityManager.find(PurchaseOrder.class, Long.valueOf(tempPOID));
        return po.getUploadedImage();
    }

    @Override
    public RawMaterialPurchaseOrder getRawMaterialPurchaseOrderById(Long valueOf) {
        return entityManager.find(RawMaterialPurchaseOrder.class, valueOf);
    }
}
